package com.xkcoding.helloworld.coding;


import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @description: 给定字符串，输出字符串中出现最多且最先出现的字符
 * @author: zhang peng
 * @date: 2021/7/25
 */
public class code1 {

    public static void main(String[] args) {
        String param = "ststts";
        char result =  getMaxOccurChar(param);
        System.out.printf(" "+result);

    }

    /**
     * @description: 输出字符串中出现最多且最先出现的字符
     * @author: zhang peng
     * @date: 2021/7/25
     * @param:
     * @return: char
     */
    public static char getMaxOccurChar(String param) {
        int maxCount = 1;
        Character result =new Character(param.charAt(0));
        Map<Character,Integer> map= new LinkedHashMap<>();
        for (int i = 0; i < param.length(); i++) {
            Character item = param.charAt(i);
            Integer count = map.get(item);
            if(null == count){
                map.put(item, 1);
            }else {
                map.put(item, count+1);
            }
        }
        //遍历
        for ( Map.Entry<Character,Integer> entry : map.entrySet()){
            if(entry.getValue() > maxCount){
                result = entry.getKey();
                maxCount = entry.getValue();
            }
        }
        return result;
    }
}
